﻿using System;
using System.ComponentModel;
using System.Windows.Forms;
using TestStepsEditor.Actions;
using TestStepsEditor.Viewers;
using TestStepsEditor.Viewers.Data;

namespace TestStepsEditor
{
    public partial class AttachmentForm : Form
    {
        private BindingList<AttachmentViewer> attachmentViewer = new BindingList<AttachmentViewer>();

        public AttachmentForm()
        {
            InitializeComponent();
        }

        public AttachmentForm(SharedData data)
        {
            InitializeComponent();
            attachmentViewer = TestCaseAttachments.Load(data);
            attachmentDataGridView.DataSource = attachmentViewer;
// ReSharper disable PossibleNullReferenceException
            attachmentDataGridView.Columns["FileName"].ReadOnly = true;
// ReSharper restore PossibleNullReferenceException
// ReSharper disable PossibleNullReferenceException
            attachmentDataGridView.Columns["FileLocation"].Visible = false;
// ReSharper restore PossibleNullReferenceException
// ReSharper disable PossibleNullReferenceException
            attachmentDataGridView.Columns["StepIndex"].Visible = false;
// ReSharper restore PossibleNullReferenceException
            attachmentDataGridView.AutoResizeColumns();
            Data = data;
        }

        public SharedData Data { get; set; }

        private void openButton_Click(object sender, EventArgs e)
        {
            TestCaseAttachments.Edit(attachmentDataGridView);
        }

        private void addButton_Click(object sender, EventArgs e)
        {
            attachmentViewer = TestCaseAttachments.Add(Data, attachmentViewer);
        }

        private void deleteButton_Click(object sender, EventArgs e)
        {
            attachmentViewer = TestCaseAttachments.Remove(attachmentDataGridView, attachmentViewer);
        }

        private void OKButton_Click(object sender, EventArgs e)
        {
            Data = TestCaseAttachments.Save(Data, attachmentViewer);
            Close();
        }

        private void cancelButton_Click(object sender, EventArgs e)
        {
            Close();
        }
    }
}